package com.turing.post.antimoney.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;

import com.turing.post.antimoney.util.AlarmInfo;
import com.turing.post.antimoney.util.AlarmInfoDao;
import com.turing.post.antimoney.util.AlarmInfoDetail;
import com.turing.post.antimoney.util.AlarmInfoDetailDao;
import com.turing.post.antimoney.util.AlarmRule;
import com.turing.post.antimoney.util.AlarmRulesBusiness;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.salesmanage.util.PersonalCustom;
import com.turing.post.salesmanage.util.PersonalCustomDao;
import com.turing.post.transaction.util.MchTradDetailDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * 同一卡号，一个月内刷卡消费累计超过N元
 * 
 * @author kangzhe
 * 
 */
@Remote
@Local
@Stateless(mappedName = "antimoney.impl.AlarmRule17")
public class AlarmRule17 implements AlarmRulesBusiness {

	@EJB(mappedName = "transaction.impl.MchTradDetailDaoImpl")
	private MchTradDetailDao mchTradDao;

	@EJB(mappedName = "antimoney.impl.AlarmInfoDaoImpl")
	private AlarmInfoDao alarmInfoDaoImpl;

	@EJB(mappedName = "antimoney.impl.AlarmInfoDetailDaoImpl")
	private AlarmInfoDetailDao alarmInfoDetailDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	@EJB(mappedName = "salesmanage.impl.PersonalCustomDaoImpl")
	private PersonalCustomDao pcDao;

	@Override
	@TransactionAttribute(javax.ejb.TransactionAttributeType.REQUIRES_NEW)
	public void runAlarmRules(String issueCompanyNo, Date tranDate,
			AlarmRule alarmRule) throws PCPSApplicationException {
		PCPSUtil.Log("执行预警规则:同一卡号，一个月内刷卡消费累计超过N元");
		// System.out.println("执行预警规则:同一实名制个人客户，同账号N天内刷卡消费累计超过N元");
		// 1.取预警参数
		if (tranDate == null || alarmRule == null
				|| PCPSUtil.isEmpty(alarmRule.getRuleParameter())) {
			PCPSUtil.Log("预警规则为空或交易日期为空!");
			// System.out.println("预警规则为空或交易日期为空!");
			return;
		}
		String[] parameters = alarmRule.getRuleParameter().split(":");
		if (parameters.length <= 0 || PCPSUtil.isEmpty(parameters[0])) {
			PCPSUtil.Log("预警规则参数为空!");
			// System.out.println("预警规则参数为空!");
			return;
		}
		// 2.计算起始交易日期,格式yyyy-mm-dd
		String startDate = null;
		String endDate = null;
		startDate = PCPSUtil.BeforeNowDate(tranDate, 0 - Integer
				.valueOf(parameters[0]));
		endDate = PCPSUtil.getPackedYMDString2(tranDate);
		// 3.取预警交易金额阀值
		BigInteger amount = BigInteger.ZERO;
		if (!PCPSUtil.isEmpty(parameters[1])) {
			amount = new BigInteger(parameters[1]);
		}
		// 4.发布预警信息
		releaseAlarm(issueCompanyNo, alarmRule, startDate, endDate, amount,
				tranDate);
		PCPSUtil.Log("执行完成!");
		// System.out.println("执行完成!");
	}

	/**
	 * 发布预警信息
	 * 
	 * @param issueCompanyNo
	 * @param alarmRule
	 * @param startDate
	 * @param endDate
	 * @param amount
	 * @throws PCPSApplicationException
	 */
	private void releaseAlarm(String issueCompanyNo, AlarmRule alarmRule,
			String startDate, String endDate, BigInteger amount, Date tranDate)
			throws PCPSApplicationException {
		// 1.获取同一持卡人不同卡号与主账号集合
		List<Object[]> amountLimitCards = mchTradDao
				.getCardsAmountLimitFromMrchTradDetail(issueCompanyNo,
						startDate, endDate, amount);
		if (amountLimitCards == null || amountLimitCards.size() <= 0) {
			return;
		}
		int cardsSize = amountLimitCards.size();
		Object[] cardObj = null;
		// System.out.println("ownerCardsSize=" + ownerCardsSize);
		for (int i = 0; i < cardsSize; i++) {
			cardObj = amountLimitCards.get(i);
			// 2.查询出符合预警条件的卡号
			List<Object[]> mchTradList = mchTradDao.getMrchTradDetailByCardNo(
					issueCompanyNo, startDate, endDate, cardObj[0].toString());
			if (mchTradList == null || mchTradList.size() <= 0) {
				continue;
			}

			// 3.添加预警信息
			AlarmInfo alarmInfo = null;
			AlarmInfoDetail alarmInfoDetail = null;
			// 风险卡号
			String riskmonCardNo = "";
			// 预警明细序号
			int seqno = 1;
			for (int j = 0; j < mchTradList.size(); j++) {
				Object[] obj = mchTradList.get(j);
				if (!riskmonCardNo.equals(obj[0].toString())) {
					// 插入预警信息
					alarmInfo = new AlarmInfo();
					riskmonCardNo = obj[0].toString();
					seqno = 1;

					alarmInfo.setAlarmInfoId(alarmInfoDaoImpl
							.getMaxAlarmInfoId());
					alarmInfo.setAlarmRuleId(alarmRule.getAlarmRuleId());
					alarmInfo.setIssueCompanyNo(issueCompanyNo);
					alarmInfo.setAlarmTime(commonDaoImpl.getDatabaseSysTime());
					alarmInfo.setBranchNo(obj[0].toString());
					alarmInfo.setBranchName(obj[1].toString());
					alarmInfo.setRiskCustomName(obj[3].toString());
					alarmInfo.setAlarmReason(alarmRule.getAlarmRuleDesc());
					alarmInfo.setDealStatus("0");
					alarmInfoDaoImpl.insertAlarmInfo(alarmInfo);
					// 更新风险客户等级为:中等风险客户
					PersonalCustom pc = pcDao
							.getPersonalCustomByPersonalId(new BigInteger(
									obj[4].toString()));
					pc.setRiskLevel(PersonalCustom.HIGH_CUSTOM);
					pcDao.updatePersonalCustom(pc);
				}
				// 插入预警明细
				alarmInfoDetail = new AlarmInfoDetail();
				alarmInfoDetail.setAlarmInfoId(alarmInfo.getAlarmInfoId());
				alarmInfoDetail.setSeqNo(seqno);
				seqno++;
				alarmInfoDetail.setBusinessType("3");
				alarmInfoDetail.setMerchantNo(obj[0].toString());
				alarmInfoDetail.setTranDate(tranDate);
				alarmInfoDetail.setTranSeqNo(obj[2].toString());
				alarmInfoDetailDaoImpl.insertAlarmInfoDetail(alarmInfoDetail);
			}
		}
	}
}
